package cn.dankal.lequ.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.BaseAdapter.ViewHolder;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.CommonAdapter;
import cn.dankal.lequ.commom.adapter.RecyclerViewAdapter;
import cn.dankal.lequ.commom.util.Constant;
import cn.dankal.lequ.commom.util.MyList;

/**
 * 作者： fred on 16/4/18 16:46.
 * 邮箱：fred_kang@dankal.cn
 */

public class RankingActivity extends AppCompatActivity {
    @Bind(R.id.tab_layout)
    TabLayout tab_layout;
    @Bind(R.id.view_pager)
    ViewPager view_pager;

    int currentPos = 0;

    RecyclerView recyclerview, recyclerview1, recyclerview2, recyclerview3;

    LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        ButterKnife.bind(this);
        currentPos = getIntent().getIntExtra("pagePosition", 0);
        initTabLayout();
    }

    @OnClick(R.id.bt_back)
    public void bt_back(View view) {
        this.finish();
    }

    private void initTabLayout() {
        MyList<RecyclerView> recyclerViews = new MyList<>();
        initRecyclerView();
        initRecyclerView1();
        initRecyclerView2();
        initRecyclerView3();

        MyList<String> tablayout_titles = new MyList<>();
        recyclerViews.add(recyclerview, recyclerview1, recyclerview2, recyclerview3);
        tablayout_titles.add(Constant.REMENBANG, Constant.WANGYOU, Constant.XINYOU, Constant.DANJI);
        for (int i = 0; i < tablayout_titles.size(); i++) {
            tab_layout.addTab(tab_layout.newTab().setText((String) tablayout_titles.get(i)));
        }
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(recyclerViews, tablayout_titles);
        view_pager.setAdapter(adapter);
        view_pager.setCurrentItem(currentPos);
        tab_layout.setupWithViewPager(view_pager);
        tab_layout.setTabsFromPagerAdapter(adapter);
    }


    private void initRecyclerView() {
        recyclerview = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        mDatas.add(2);
        mDatas.add(3);
        mDatas.add(4);
        mDatas.add(5);
        mDatas.add(6);
        mDatas.add(7);
        mDatas.add(8);
        recyclerview.setAdapter(new CommonAdapter<Integer>(this, R.layout.item_youxibang, mDatas) {
            @Override
            public void convert(ViewHolder holder, Integer i) {
                if (i != 1 && i != 2 && i != 3)
                    holder.setText(R.id.tv_position, Integer.toString(i));
                if (i == 1)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang1);
                else if (i == 2)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang2);
                else if (i == 3)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang3);
            }
        });
    }

    private void initRecyclerView1() {
        recyclerview1 = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview1.setLayoutManager(linearLayoutManager);
        recyclerview1.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        recyclerview1.setAdapter(new CommonAdapter<Integer>(this, R.layout.item_youxibang, mDatas) {
            @Override
            public void convert(ViewHolder holder, Integer i) {
                if (i != 1 && i != 2 && i != 3)
                    holder.setText(R.id.tv_position, Integer.toString(i));
                if (i == 1)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang1);
                else if (i == 2)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang2);
                else if (i == 3)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang3);
            }
        });
    }

    private void initRecyclerView2() {
        recyclerview2 = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview2.setLayoutManager(linearLayoutManager);
        recyclerview2.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        mDatas.add(2);
        mDatas.add(3);
        mDatas.add(4);
        recyclerview2.setAdapter(new CommonAdapter<Integer>(this, R.layout.item_youxibang, mDatas) {
            @Override
            public void convert(ViewHolder holder, Integer i) {
                if (i != 1 && i != 2 && i != 3)
                    holder.setText(R.id.tv_position, Integer.toString(i));
                if (i == 1)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang1);
                else if (i == 2)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang2);
                else if (i == 3)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang3);
            }
        });
    }

    private void initRecyclerView3() {
        recyclerview3 = new RecyclerView(this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerview3.setLayoutManager(linearLayoutManager);
        recyclerview3.setHasFixedSize(true);

        List<Integer> mDatas = new ArrayList<>();
        mDatas.add(1);
        mDatas.add(2);
        mDatas.add(3);
        mDatas.add(4);
        mDatas.add(5);
        mDatas.add(6);
        recyclerview3.setAdapter(new CommonAdapter<Integer>(this, R.layout.item_youxibang, mDatas) {
            @Override
            public void convert(ViewHolder holder, Integer i) {
                if (i != 1 && i != 2 && i != 3)
                    holder.setText(R.id.tv_position, Integer.toString(i));
                if (i == 1)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang1);
                else if (i == 2)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang2);
                else if (i == 3)
                    holder.setBackgroundRes(R.id.tv_position, R.mipmap.youxibang3);
            }
        });
    }

}
